import { auditCostPage, auditCost, finishCost, rejectCost, feeSum, updateAccount } from '../services/dispatch';
import React from 'react';
import { Input, message, Select } from 'antd';

// 表格列信息
const columns = [
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '90px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机姓名',
    dataIndex: 'driver',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机电话',
    dataIndex: 'drPhone',
    width: '110px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机身份证',
    dataIndex: 'drIdcard',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '账户姓名',
    dataIndex: 'accountName',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '银行卡号',
    dataIndex: 'bankAccount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '费用状态',
    dataIndex: 'status',
    width: '80px',
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'APPLY':
          return <span>申请</span>;
        case 'PASS':
          return <span>通过</span>;
        case 'APPLY_PAY':
          return <span>申请支付</span>;
        case 'URGENT_PAY':
          return <span>紧急支付</span>;
        case 'PAY':
          return <span>支付</span>;
        case 'PAY_ALLOW':
          return <span>可支付</span>;
        case 'PAY_FINISH':
          return <span>支付完成</span>;
        case 'CANCEL':
          return <span>作废</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '实际车型',
    dataIndex: 'realCarType',
    width: '75px',
  },
  {
    title: '实际体积',
    dataIndex: 'realVoPrice',
    width: '75px',

  },
  {
    title: '实际重量',
    dataIndex: 'realWePrice',
    width: '75px',
  },
  {
    title: '体积单价',
    width: '75px',
    render: (text, r) => {
      if(r.realVoPrice && r.realVoPrice > 0){
        return (r.arteryFee / r.realVoPrice).toFixed(2)
      }
    }
  },
  {
    title: '重量单价',
    width: '75px',
    render: (text, r) => {
      if(r.realWePrice && r.realWePrice > 0){
        return (r.arteryFee / r.realWePrice).toFixed(2)
      }
    }
  },
  {
    title: '合计',
    dataIndex: 'feeSum',
    width: '75px',
  },
  {
    title: '提货费',
    dataIndex: 'pickFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '干线费',
    dataIndex: 'arteryFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '送货费',
    dataIndex: 'dropoffFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加点费',
    dataIndex: 'workoverFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货费',
    dataIndex: 'loadupFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货费',
    dataIndex: 'loadoffFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '包车费',
    dataIndex: 'carFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '燃油补助费',
    dataIndex: 'fuelFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加班费',
    dataIndex: 'callbackFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '押车费',
    dataIndex: 'escortinFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '小费',
    dataIndex: 'tipFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '短驳费',
    dataIndex: 'shuttleFee',
    width: '75px',
    editable: true,
    ellipsis: true,
  },
];

// 搜索框信息
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '车牌号',
        name: 'carNo',
        component: <Input placeholder="请输入车牌号" />,
      },
      {
        md: 6,
        label: '司机姓名',
        name: 'driver',
        component: <Input placeholder="请输入司机姓名" />,
      },
      {
        md: 6,
        label: 'load号',
        name: 'loadNo',
        component: <Input placeholder="请输入load号" />,
      },
      {
        md: 6,
        label: '状态',
        name: 'status',
        component: (
          <Select placeholder="请输入支付状态">
            <Option value="APPLY">申请</Option>
            <Option value="PASS">通过</Option>
          </Select>
        ),
      },
    ],
  },
];

//子表格列表信息
const columnsChlidren = [
  {
    title: '项目简称',
    dataIndex: 'customName',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: 'load号',
    dataIndex: 'loadNo',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '提货日期',
    dataIndex: 'pickDate',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '到货日期',
    dataIndex: 'aogDate',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货工厂',
    dataIndex: 'loadingFactory',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货地',
    dataIndex: 'loadingPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货工厂',
    dataIndex: 'loadoffFactory',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货地',
    dataIndex: 'loadoffPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '业务类型',
    dataIndex: 'busType',
    width: '80px',
    editable: true,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'LD':
          return <span>零担</span>;
        case 'DC':
          return <span>单程</span>;
        case 'WF':
          return <span>往返</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '要车类型',
    dataIndex: 'carType',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '数量',
    dataIndex: 'amount',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '体积',
    dataIndex: 'volume',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '重量',
    dataIndex: 'weight',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
 /* {
    title: '体积单价',
    dataIndex: 'vprice',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '重量单价',
    dataIndex: 'wprice',
    width: '80px',
    editable: true,
    ellipsis: true,
  },*/
  {
    title: '提货费',
    dataIndex: 'pickFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '干线费',
    dataIndex: 'arteryFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '送货费',
    dataIndex: 'dropoffFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加点费',
    dataIndex: 'workoverFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货费',
    dataIndex: 'loadupFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货费',
    dataIndex: 'loadoffFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '包车费',
    dataIndex: 'carFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '燃油补助费',
    dataIndex: 'fuelFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加班费',
    dataIndex: 'callbackFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '押车费',
    dataIndex: 'escortinFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '小费',
    dataIndex: 'tipFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '短驳费',
    dataIndex: 'shuttleFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '合计费用',
    dataIndex: 'feeSum',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
];

export default {
  namespace: 'auditCost',

  state: {
    columns: columns, // 费用表头
    searchFields: searchFields, //搜索框信息
    searchValues: {
      status: {
        name: 'status',
        value: 'APPLY',
      }
    }, // 搜索框里的值
    searchValueAuditPay: {
      status: {
        name: 'status',
        value: 'APPLY_PAY',
      }
    },
    searchValuesPay: {
      status: {
        name: 'status',
        value: 'PAY_ALLOW',
      }
    },
    searchValuesPayAllow: {
      status: {
        name: 'status',
        value: 'PAY'
      }
    },
    pageList: [], // 表格数据
    childrenTable: [], // 子表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
      pageSizeOptions: ['10', '20', '30', '40', '100']
    }, // 表格分布数据
    columnsChlidren: columnsChlidren, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    selectionOrderIds: [], // ORDER表格选择ID
    sumFee: 0.00, // 总费用合计
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(auditCostPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *audit({ payload }, { call, put }) {
      const res = yield call(auditCost, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *finish({ payload }, { call, put }) {
      const res = yield call(finishCost, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *reject({ payload }, { call, put }) {
      const res = yield call(rejectCost, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *feeSum({ payload }, { call, put }) {
      const res = yield call(feeSum, payload);
      yield put({
        type: 'feeSum_r',
        payload: res,
      })
    },

    *updateAccount({ payload }, { call, put }) {
      const res = yield call(updateAccount, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      })
    }

  },
  reducers: {
    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 绑定搜索框数据
    handlePaySearchChange(state, action) {
      const { searchValuesPay } = state;
      const newSearchForm = {
        ...searchValuesPay,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesPay: newSearchForm,
      };
    },
    handlePayAllowSearchChange(state, action) {
      const { searchValuesPayAllow } = state;
      const newSearchForm = {
        ...searchValuesPayAllow,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesPayAllow: newSearchForm,
      };
    },
    handlePayAuditSearchChange(state, action) {
      const { searchValueAuditPay } = state;
      const newSearchForm = {
        ...searchValueAuditPay,
        ...action.payload,
      };
      return {
        ...state,
        searchValueAuditPay: newSearchForm,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
        };
      } else {
        return {
          ...state,
        };
      }
    },
    audit_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('操作成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '操作失败！');
      }
      return {
        ...state,
      };
    },
    feeSum_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          feeSum: payload.result
        }
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '合计费用失败！');
      }
      return {
        ...state,
      };
    }
  },
};
